layui.use(['upload','form','layer'], function() {
    createSelect()
    var $ = layui.jquery
        , upload = layui.upload;
    let form = layui.form;
    let layer = layui.layer
    let pic =$('#head_pic')

    if(pic.attr('src')=='#'){
        pic.hide()
    }

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        , url: '/upload' //改成您自己的上传接口
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {


            });
        }
        , done: function (res) {
            let result = res.img
            console.log(res.img)
            $('#head_pic').attr('src', result); //图片链接（base64）
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            //上传成功
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });

    form.on('submit(add)',function (data) {
        data = data.field;
        data.head_pic = $('#head_pic').attr('src')
        console.log('src:'+$('#head_pic').attr('src'))
        console.log('data:'+data.head_pic )
        data.loginName=sessionStorage.getItem("loginName")
        $.ajax({
            url:'/back/lawyer/add',
            data:data,
            dataType:'json',
            type:'post',
            success:function (data) {
                console.log(data);
                if (data.result == "success") {
                    //发异步，把数据提交给php
                    layer.alert("增加成功", {
                            icon: 6
                        },
                        function () {
                            //关闭当前frame
                            xadmin.close();

                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                }
                if(data.result=="notLogin"){
                    window.open("/html/login.html", "_top");
                }
                else {
                    layer.alert("增加失败", {
                        icon: 1
                    });
                }
            }

        });
        return false;
    })


})



function createSelect() {
    let select = $("#role")


    layui.use('form',function () {

        $.ajax({
            url:'/back/role/all',
            dataType:'json',
            type:'get',
            data:{loginName:sessionStorage.getItem("loginName")},
            success:function (res) {
                if(res.result=="notLogin"){
                    window.open("/html/login.html", "_top");
                }
                select.empty()
                let data =res.roles;
                $.each(data,function (index, item) {

                    select.append(new Option(item.role,item.role))
                    console.log(select)
                })
                layui.form.render("select");
            }
        })
    })
}




